import Vue from 'vue/dist/vue.esm.js';
import Vuex from 'vuex';
import { Base64 } from '@common/base64.min.js';
import { doRegister, doLogin, doLoginByCode, getToken } from '@doapis/user/user.js';

Vue.use(Vuex);

export default new Vuex.Store({
	state: {
		userDATA: $.cookie('userDATA') || {},
		personalMenuKEY: $.$localStorage.getItem('personalMenuKEY', true) || {}
	},
	mutations: {
		pushUserData: function(state, data) {
			const userDATA = data.userDATA;
			data = $.extend(state.userDATA, data.userDATA);
			// 存放原始手机邮箱
			if (userDATA.mobilePhone) data[Base64.encode('mobilePhone')] = data.mobilePhone;
			if (userDATA.email) data[Base64.encode('email')] = data.email;
			if (userDATA.mobileEmail) data[Base64.encode('mobileEmail')] = data.mobileEmail;
			// 给手机邮箱隐藏中间的数据
			data.mobileEmail = $.$hideTheMiddle(userDATA.mobileEmail);
			data.mobilePhone = userDATA.email ?  $.$hideTheMiddle(data.email) : $.$hideTheMiddle(data.mobilePhone);
			data.email = $.$hideTheMiddle(data.email);

			$.cookie('userDATA', data, { expires: 30 });
			state.userDATA = data;
		},
		removeUserData: function(state, data) {
			state.userDATA = data.userDATA;
		},
		togglePersonalMenuKEY: function(state, data) {
			data = data.data || {};
			$.$localStorage.setItem('personalMenuKEY', data);
			state.personalMenuKEY = data;
		},
	},
	actions: {
		// 注册
		doRegister: function(ctx, data) {
			let userDATA = data.userDATA;
			return doRegister(userDATA).then(function(res){
				if (res.success) {
					const data = res.data;
					data.cardNumber = $.$hideTheMiddle(data.cardNumber);
					data.password = Base64.encode(data.password);
					data.mobileEmail = userDATA.mobileEmail;

					ctx.commit({
						type: 'pushUserData',
						userDATA: data
					});
				}
			});
		},
		// 登录
		doLogin: function(ctx, data) {
			let userDATA = data.userDATA;
			const loginMethod = userDATA.selectType === 1 ? doLogin : doLoginByCode;
			userDATA.tenantId = 'GRINOK';
			return loginMethod(userDATA).then(function(res){
				if (res.success) {
					const data = res.data;
					if (userDATA.selectType === 1) {
						data.isRemember = userDATA.isRemember;
						data.password = Base64.encode(userDATA.password);
					}
					data.cardNumber = $.$hideTheMiddle(data.cardNumber);
					data.mobileEmail = userDATA.mobileEmail;

					ctx.commit({
						type: 'pushUserData',
						userDATA: data
					});
					// ctx.dispatch({
					// 	type: 'getToken',
					// 	data: null
					// });
				}
			});
		},
		// 退出
		doLogout: function(ctx, { data }) {
			let result = $.removeCookie('userDATA');
			data = data || {};

			if (result) {
				ctx.commit({
					type: 'removeUserData',
					userDATA: null
				});

				if (!data.notAlert) {
					Vue.$modal.success({
						title: $.i18n_t('common.alert.success.title'),
						content: $.i18n_t('common.alert.success.exit'),
						onOk: function() {
							window.location.reload();
						}
					});
				}
			}
		},
		// 更改密码
		getToken: function(ctx, data) {
			let userDATA = data.userDATA;
			return getToken(userDATA);
		},
	}
});
